package com.ag.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 
 * title: BigDecimals.java 
 *
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0  
 * @created Mar 8, 2021 1:46:04 PM
 */
public class BigDecimals {

	/**
	 * 不等于 0
	 * @param o
	 * @return
	 */
	public static boolean neZero(Object o) {
		if(o == null) {
			return false;
		}
		return ! eqZero(o);
	}
	
	/**
	 * 等于 0 
	 * @param o
	 * @return
	 */
	public static boolean eqZero(Object o) {
		return eq(o, BigDecimal.ZERO);
	}
	
	/**
	 * 大于等于 0
	 * @param o
	 * @return
	 */
	public static boolean goeZero(Object o) {
		return goe(o, BigDecimal.ZERO);
	}

	/**
	 * 大于 0
	 * @param o
	 * @return
	 */
	public static boolean gtZero(Object o) {
		return gt(o, BigDecimal.ZERO);
	}
	
	/**
	 * 小于 0
	 * @param o
	 * @return
	 */
	public static boolean ltZero(Object o) {
		BigDecimal bd = p(o);
		return bd.compareTo(BigDecimal.ZERO) < 0;
	}
	
	/**
	 * 小于等于 0
	 * @param o
	 * @return
	 */
	public static boolean loeZero(Object o) {
		return loe(o, BigDecimal.ZERO);
	}

	public static boolean eq(Object f, Object s) {
		return p(f).compareTo(p(s)) == 0;
	}
	
	public static boolean ne(Object f, Object s) {
		return ! eq(f, s);
	}
	
	public static boolean goe(Object f, Object s) {
		return p(f).compareTo(p(s)) >= 0;
	}

	public static boolean gt(Object f, Object s) {
		return p(f).compareTo(p(s)) > 0;
	}
	
	public static boolean lt(Object f, Object s) {
		return p(f).compareTo(p(s)) < 0;
	}
	
	public static boolean loe(Object f, Object s) {
		return p(f).compareTo(p(s)) <= 0;
	}

	/**
	 * 加
	 * @param o
	 * @return
	 */
	public static BigDecimal add(Object... o) {
		BigDecimal sum = BigDecimal.ZERO;
		for (Object _o : o) {
			if (null == _o) {
				continue;
			}
			sum = sum.add(p(_o));
		}
		return sum;
	}
	
	public static BigDecimal lAdd(List<? extends Object> o) {
		BigDecimal sum = BigDecimal.ZERO;
		for (Object _o : o) {
			if (null == _o) {
				continue;
			}
			sum = sum.add(p(_o));
		}
		return sum;
	}
	
	public static BigDecimal lAdd(Stream<? extends Object> o) {
		List<? extends Object> list = o.collect(Collectors.toList());
		return lAdd(list);
	}

	/**
	 * 减
	 * @param o
	 * @return
	 */
	public static BigDecimal subtract(Object cardinal, Object... o) {
		BigDecimal sum = p(cardinal);
		for (Object c : o) {
			sum = sum.subtract(p(c));
		}
		return sum;
	}
	
	public static BigDecimal lSubtract(Object cardinal, List<?> o) {
		BigDecimal sum = p(cardinal);
		for (Object c : o) {
			sum = sum.subtract(p(c));
		}
		return sum;
	}

	/**
	 * 乘法
	 * 
	 * @param o
	 * @return
	 */
	public static BigDecimal multiplys(Object... o) {
		BigDecimal sum = BigDecimal.ONE;
		for (Object c : o) {
			sum = sum.multiply(p(c));
		}
		return sum;
	}

	/**
	 * 除
	 * 
	 * @param cardinal
	 * @param divisiend
	 * @return
	 */
	public static BigDecimal divide(Object cardinal, Object divisiend) {
		return p(cardinal).divide(p(divisiend), 10, RoundingMode.HALF_UP);
	}

	/**
	 * 将对象转 BigDecimal
	 * 
	 * @param o 如果o==null这返回 BigDecimal.ZERO
	 * @return
	 */
	public static BigDecimal p(Object o) {
		if(o == null) {
			return BigDecimal.ZERO;
		}
		
		if(o instanceof BigDecimal) {
			return (BigDecimal) o;
		} else if(o instanceof String) {
			return new BigDecimal((String) o);
		}
		
		Double d = parse(o);
		return BigDecimal.valueOf(d);
	}
	
	/**
	 * 将对象转 BigDecimal
	 * 
	 * @param o 如果o==null这返回 BigDecimal.ZERO
	 * @return
	 */
	public static BigDecimal p(Object o, BigDecimal def) {
		try {
			return p(o);
		}catch(Exception e) {
			return def;
		}
	}

	public static Double parse(Object o) {
		if (o == null) {
			return 0D;
		} else if (o instanceof Number) {
			return ((Number) o).doubleValue();
		} 
		
		return Double.parseDouble(o.toString());
	}

	public static void main(String[] args) {

		// System.out.println(BigDecimalUtils.subtract(0,-2));
		System.out.println(BigDecimals.p(1505.0100000).toString());
	}
}
